<?php
/*
 * 支付宝当面付极简版（生成付款链接）
 * 只需配置以下三个参数：
 */
$config = [
    'app_id'      => '',  // 应用APPID
    'private_key' => <<<EOD
-----BEGIN RSA PRIVATE KEY-----
应用私钥
-----END RSA PRIVATE KEY-----
EOD,
    'alipay_public_key' => <<<EOD
-----BEGIN PUBLIC KEY-----
支付宝公钥
-----END PUBLIC KEY-----
EOD
];


//------------------------- 以下代码无需修改 -------------------------

// 自动生成订单信息
$order = [
    'out_trade_no' => date('YmdHis').mt_rand(1000,9999),
    'total_amount' => $_GET['money'],
    'subject'      => '电商购物'
];

// 构造请求参数
$params = [
    'app_id'      => $config['app_id'],
    'method'      => 'alipay.trade.precreate',
    'charset'     => 'utf-8',
    'sign_type'   => 'RSA2',
    'timestamp'   => date('Y-m-d H:i:s'),
    'version'     => '1.0',
    'biz_content' => json_encode($order)
];

// 生成签名
ksort($params);
$signStr = urldecode(http_build_query($params));

// 验证私钥有效性
if(!$privateKey = openssl_pkey_get_private($config['private_key'])){
    die("私钥错误：".openssl_error_string());
}

openssl_sign($signStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
$params['sign'] = base64_encode($sign);

// 发送请求
$gateway = 'https://openapi.alipay.com/gateway.do';
$response = json_decode(file_get_contents($gateway.'?'.http_build_query($params)), true);

// 输出结果
if(isset($response['alipay_trade_precreate_response']['qr_code'])){
    echo "电商付款码:".$response['alipay_trade_precreate_response']['qr_code'];
} else {
    echo "生成失败，错误信息：<pre>";
    print_r($response);
}
?>
<html>
<head>
    <title>url-pay</title>
    <meta charset="UTF-8">
    <meta name="google" value="notranslate">
  <meta name="baidu-site-verification" content="0fFS5DZPGS" />
  <meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
</head>
<body>
 <!-- 前端显示示例 -->
 <center>
<img src="https://qun.qq.com/qrcode/index?&size=300&data=<? echo $response['alipay_trade_precreate_response']['qr_code']?>">
</body>
</html>